<template>
  <div>
    <a-modal
      v-model:open="showModal"
      :mask-closable="false"
      :show-icon="false"
      :style="{
        width: dialogWidth,
      }"
      :title="formValue.id > 0 ? '编辑帮助中心 #' + formValue.id : '添加帮助中心'"
      :z-index="99"
      preset="card"
      transform-origin="center"
      @ok="confirmForm"
    >
      <n-scrollbar class="pr-5" style="max-height: 87vh">
        <n-spin :show="loading" description="请稍候...">
          <n-form
            ref="formRef"
            :label-placement="settingStore.isMobile ? 'top' : 'left'"
            :label-width="100"
            :model="formValue"
            :rules="rules"
            class="py-4"
          >
            <n-grid cols="1 s:1 m:1 l:1 xl:1 2xl:1" responsive="screen">
              <n-gi span="1">
                <n-form-item label="语言" path="language">
                  <n-radio-group v-model:value="formValue.language" name="language"  @update:value="handleUpdateLanguageValue">
                    <n-radio-button
                      v-for="language in options.language"
                      :key="language.value"
                      :label="language.label"
                      :value="language.value"
                    />
                  </n-radio-group>
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="选择分类" path="categoryId">
                  <n-select
                    v-model:value="formValue.categoryId"
                    :options="options.category"
                    clearable
                    filterable
                    label-field="name"
                    value-field="id"
                  />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="标题" path="title">
                  <n-input v-model:value="formValue.title" placeholder="请输入标题" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="内容" path="content">
                  <Editor id="content" v-model:modelValue="formValue.content" style="height: 450px" />
                </n-form-item>
              </n-gi>
              <n-gi span="1">
                <n-form-item label="排序" path="sort">
                  <n-input-number
                    v-model:value="formValue.sort"
                    clearable
                    placeholder="请输入排序"
                    style="width: 100%"
                  />
                  <template #feedback>
                    <div style="font-size: 12px">越大越靠前</div>
                  </template>
                </n-form-item>
              </n-gi>
            </n-grid>
          </n-form>
        </n-spin>
      </n-scrollbar>
      <template #action>
        <n-space>
          <n-button @click="closeForm">
            取消
          </n-button>
          <n-button :loading="formBtnLoading" type="info" @click="confirmForm">
            确定
          </n-button>
        </n-space>
      </template>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
import {computed, ref} from 'vue';
import {Edit, View} from '@/api/pmsHelpcenter';
import {newState, options, rules, State} from './model';
import Editor from '@/components/Editor/editor.vue';
import {useProjectSettingStore} from '@/store/modules/projectSetting';
import {useMessage} from 'naive-ui';
import {adaModalWidth} from '@/utils/hotgo';
import {All} from "@/api/pmsHelpcenterCategory/index";

const emit = defineEmits(['reloadTable']);
const message = useMessage();
const settingStore = useProjectSettingStore();
const loading = ref(false);
const showModal = ref(false);
const formValue = ref<State>(newState(null));
const formRef = ref<any>({});
const formBtnLoading = ref(false);
const dialogWidth = computed(() => {
  return adaModalWidth(840);
});

function openModal(state: State) {
  showModal.value = true;

  // 新增
  if (!state || state.id < 1) {
    formValue.value = newState(state);

    return;
  }

  // 编辑
  loading.value = true;

  All({
    language: state.language,
  }).then((res) => {
    options.value.category = res.list;
  });

  View({ id: state.id })
    .then((res) => {
      formValue.value = res;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleUpdateLanguageValue(value) {
  All({
    language: value,
  }).then((res) => {
    formValue.value.categoryId = null
    options.value.category = res.list;
  });
}

function confirmForm(e) {
  e.preventDefault();
  formBtnLoading.value = true;
  formRef.value.validate((errors) => {
    if (!errors) {
      Edit(formValue.value).then((_res) => {
        message.success('操作成功');
        setTimeout(() => {
          formBtnLoading.value = false;
          closeForm();
          emit('reloadTable');
        });
      }).catch((err) => {
        formBtnLoading.value = false;
      });
    } else {
      message.error('请填写完整信息');
      formBtnLoading.value = false;
    }
  });
}

function closeForm() {
  showModal.value = false;
  loading.value = false;
}

defineExpose({
  openModal,
});
</script>

<style lang="less"></style>


